import { cloneDeep } from "lodash-es";
import { CustomTableColumns, CustomTableProps } from "../types";

const initFlag = (column:CustomTableColumns):any => {
    if(!column.flag) return 
    let columnRef = cloneDeep(column)
    switch (columnRef.flag) {
        case 'ACTION':
            columnRef.title = '操作'
            columnRef.fixed = 'right',
            column.bodyCell = 'action'
            break;
        case 'CHECKBOX':
            columnRef.title = '选择'
            columnRef.fixed = 'left'
    }
    return columnRef
}

export function useColumns(props: ComputedRef<CustomTableProps>) {
    const columnsRef = cloneDeep(props.value.columns)

    const getColumns = computed(() => {
        if(!columnsRef) return
        let columns = columnsRef?.map((column:CustomTableColumns) => {
            let columnRef:CustomTableColumns | {} = {}
            if(Reflect.has(column, 'flag')){
                columnRef = initFlag(column)
            }
            return {
                align: 'center',
                ellipsis: true,
                resizable: true,
                width: 90,
                ...columnRef,
                ...column
            }
        })
        return columns
    })

    return {
        getColumns
    }
}